<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>待办任务列表</title>
  <style>
    #app {
      width: 800px;
      margin: 0 auto;
      padding: 10px;
      border: 1px solid #ccc;
    }

    .content {
      width: 700px;
      margin: 0 auto;
    }

    .title {
      text-align: center;
    }

    .btn {
      height: 40px;
      width: 100px;
      margin-left: 10px;
      padding: 5px 10px;
      border: 1px solid #ccc;
      border-radius: 5px;
      background-color: #fff;
      cursor: pointer;
    }

    .all-count {
      background-color: #1363e3;
      color: #fff;
      padding: 10px 10px;
      border-radius: 5px;
    }

    .unfinish-count {
      background-color: #f4ad07;
      color: #fff;
      padding: 10px 10px;
      border-radius: 5px;
    }

    .task-input {
      width: 430px;
      height: 30px;
      padding: 5px 10px;
      border: 1px solid #ccc;
      border-radius: 5px;
      background-color: #fff;
      font-size: 16px;
    }

    .task-list {
      border-radius: 5px;
      list-style: none;
      padding: 0px;
    }

    .task-list li {
      padding: 5px;
      border: 1px solid #ccc;
      margin-top: 3px;
      border-radius: 5px;
      height: 40px;
      line-height: 40px;
    }

    .ckbox {
      width: 16px;
      height: 16px;
      cursor: pointer;
    }

    .done {
      text-decoration: line-through;
      color: #fff;
      background-color: green;
    }

    .normal {
      color: #000;
      background-color: #fff;
    }
  </style>
</head>

<body>
  <div id="app">
    <h1 class="title">我的待办任务</h1>
    <div class="content">
      <p>
        <span class="all-count">任务总数：<span v-text="Sum"></span> 个</span>
        <span class="unfinish-count">未完成：<span v-text="removeDoneTasks"></span> 个</span>
      </p>
      <p>
        <input class="task-input" placeholder="请输入新待办任务" type="text" v-model="newTask" />
        <button class="btn" @click="addClick">添加任务</button>
        <button class="btn" @click="clean"> 清除已完成</button>
      </p>
      <p>
      <h3>任务列表</h3>
      </p>
      <ul class="task-list">
        <li v-for="(item, index) in tasks" :key="item.content" 
        :class="{normal: !item.done, done: item.done }">
          <input type="checkbox" class="ckbox" :checked="item.done" v-model="item.done"/>
          <input v-if="item.editing" type="text"  @blur="handleBlur(index)" class="task-input" v-model="tasks[index].content"/>
          <span v-else  @click="toggleEdit(index)" v-text="item.content"></span>
        </li>
      </ul>
    </div>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
  <script>
    // 待办任务列表
    // 文本框中输入新任务内容，点击“添加”按钮将新任务添加到任务列表中。
    // 单击未完成任务文字，切换为输入框显示，重新编辑后，离开焦点自动修改任务内容。
    // 任务已完成可点击前面的复选框，勾选后给li标签移除normal样式，添加done样式，并且未完成任务数需要减一。
    // 顶部显示任务总数、未完成任务数。点击“清除已完成”按钮，则删除所有已完成任务
    // TODO 使用Vue完成上述需求
    new Vue({
      el: "#app",
      data: {
        newTask: "",
        flg: false,
        tasks:
          [
            { content: "学习Vue", done: false },
            { content: "学习React", done: true },
            { content: "学习Angular", done: false }
          ]
      },
      computed: {
        Sum() {
          let count = 0;
          for (let i = 0; i < this.tasks.length; i++) {
            count++;
          }
          return count;
        },
        removeDoneTasks() {
          return this.tasks.filter(task => !task.done).length;
        }
      },
      methods: {
        addClick: function () {
          if (this.newTask != null && this.newTask != "") {
            this.tasks.push({
              content: this.newTask,
              done: false
            });
            this.newTask = "";
          } else {
            alert("任务不能为空")
          }

        },
        clean() {
          for (let i = this.tasks.length - 1; i >= 0; i--) {
            if (this.tasks[i].done) {
              this.tasks.splice(i, 1);
            }
          }
        },
        // 可以添加一个方法来切换任务的编辑状态
        toggleEdit(index) {
           Vue.set(this.tasks[index], 'editing', true); 

        },
        handleBlur(index){
          Vue.set(this.tasks[index], 'editing', false);
        //在这里更新data的数组
         let updatedTask = this.tasks[index];
        }
      }
    })
  </script>
</body>

</html>